package com.tycmc.signelectroniccontracts.tools.times;

import android.content.Context;

import com.tycmc.signelectroniccontracts.R;
import com.tycmc.signelectroniccontracts.tools.times.chosetime.CustomDatePicker;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * Created by Administrator on 2016/8/9 0009.
 */
public class DataTimer {


    /**
     * 获取当前时间的Calendar
     *
     * @return
     */
    public static Calendar getNowCalendar() {
        return Calendar.getInstance();
    }



    /**
     * 获取当前的时间
     *
     * @param format 返回格式
     * @return
     */
    public static String getNowString(String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateString = formatter.format(System.currentTimeMillis());
        return dateString;
    }

    public static String getStringData(String format, long longTime) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateString = formatter.format(longTime);
        return dateString;
    }


    /**
     * 由时间转化为毫米值
     *
     * @param str 时间
     * @param sdf 时间格式
     * @return
     */
    public static long getLongTime(String str, String sdf) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(sdf);
        try {
            long time = simpleDateFormat.parse(str).getTime();
            return time;
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * @param time1 时间1
     * @param time2 时间2
     * @param sdf   时间格式
     * @return ture 时间1后 false时间2前
     */
    public static boolean comperidTwoTime(String time1, String time2, String sdf) {
        long longTime1 = getLongTime(time1, sdf);
        long longTime2 = getLongTime(time2, sdf);
        return longTime2 < longTime1;
    }


    /**
     * 将时间格式转化成Date
     *
     * @param strDate
     * @param format
     * @return
     */
    public static Date strToDate(String strDate, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    /**
     * 时间格式转换
     *
     * @param dateDate
     * @param format
     * @return
     */
    public static String dateToString(Date dateDate, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateString = formatter.format(dateDate);
        return dateString;
    }


    /**
     * 判断是否润年
     *
     * @param year
     * @return
     */
    public static boolean isLeapYear(int year) {


        if ((year % 400) == 0)
            return true;
        else if ((year % 4) == 0) {
            if ((year % 100) == 0)
                return false;
            else
                return true;
        } else
            return false;
    }


    /**
     * 获取一个月的最后一天
     *
     * @return
     */
    public static String getEndDateOfMonth(int year, int mon) {// yyyy-MM-dd
        String str = "";
        if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {
            str += "31";
        } else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
            str += "30";
        } else {
            if (isLeapYear(year)) {
                str += "29";
            } else {
                str += "28";
            }
        }
        return str;
    }

    /**
     * 判断二个时间是否在同一个周
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameWeekDates(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(date1);
        cal2.setTime(date2);
        int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
        if (0 == subYear) {
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
                return true;
        } else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
            // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
                return true;
        } else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
                return true;
        }
        return false;
    }

    /**
     * 产生周序列,即得到当前时间所在的年度是第几周
     *
     * @return
     */
    public static String getSeqWeek() {
        Calendar c = Calendar.getInstance(Locale.CHINA);
        String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
        if (week.length() == 1)
            week = "0" + week;
        String year = Integer.toString(c.get(Calendar.YEAR));
        return year + week;
    }

    /**
     * 两个时间之间的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static long getDays(String date1, String date2) {
        if (date1 == null || date1.equals(""))
            return 0;
        if (date2 == null || date2.equals(""))
            return 0;
        // 转换为标准时间
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        Date mydate = null;
        try {
            date = myFormatter.parse(date1);
            mydate = myFormatter.parse(date2);
        } catch (Exception e) {
        }
        long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
        return day;
    }

    /**
     * 当前时间往后推
     *
     * @param day 往后推的天数
     * @return
     */
    public static String delayNowDay(int day) {
        Date date = new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, 1);//把日期往后增加一天.整数往后推,负数往前移动
        date = calendar.getTime(); //这个时间就是日期往后推一天的结果
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }

    public static void choseTime(Context context, final TimerCallBack callBack, String hintDay, String beforeTime1, String endTime1, final boolean isHour) {
        long hintLong = getLongTime(hintDay, isHour?"yyyy-MM-dd HH:mm":"yyyy-MM-dd");
        long beforeLong = getLongTime(beforeTime1, isHour?"yyyy-MM-dd HH:mm":"yyyy-MM-dd");
        long endLong = getLongTime(endTime1, isHour?"yyyy-MM-dd HH:mm":"yyyy-MM-dd")+(isHour?0:24*60*60*1000-1);
        if (hintLong == 0) {
            hintDay = getNowString("yyyy-MM-dd HH:mm");
        }else{
            hintDay = getStringData("yyyy-MM-dd HH:mm", hintLong);
        }
        String beforeTime,endTime;
        if (beforeLong <= 0) {
            beforeTime = "1900-01-01 00:00";
        } else {
            beforeTime = getStringData("yyyy-MM-dd HH:mm", beforeLong);
        }
        if (endLong <= 0) {
            endTime = "2100-01-01 00:00";
        } else {
            endTime = getStringData("yyyy-MM-dd HH:mm", endLong);
        }
        if(beforeTime1.equals("1970-01-01 00:00")){
            beforeTime =getStringData("yyyy-MM-dd HH:mm", System.currentTimeMillis());
        }
        if (endTime1.equals("1970-01-01 00:00")) {
            endTime = getStringData("yyyy-MM-dd HH:mm", System.currentTimeMillis());
        }
        CustomDatePicker datePicker = new CustomDatePicker(context, isHour ? context.getString(R.string.please_chose_time) : context.getString(R.string.please_chose_data), new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) {
                if (callBack != null) {
                    callBack.callBack(isHour ? time : time.split(" ")[0]);
                }
            }
        }, beforeTime, endTime);
        datePicker.showSpecificTime(isHour);
        datePicker.setIsLoop(false);
        datePicker.show(hintDay);
    }

    /**
     * 选择日期
     *
     * @param context    上下文
     * @param callBack   回调
     * @param hintDay    时间初始值
     * @param beforeTime 日期区间
     * @param endTime    结束区间
     */
    public static void choseDay(Context context, TimerCallBack callBack, String hintDay, String beforeTime, String endTime) {

        choseTime(context, callBack, hintDay, beforeTime, endTime, false);
    }

    /**
     * 选择时间
     * @param context 上下文
     * @param callBack 回调
     * @param hintDay 初始值
     * @param beforeTime 开始时间
     * @param endTime 结束时间
     */
    public static void choseTime(Context context, TimerCallBack callBack, String hintDay, String beforeTime, String endTime) {

        choseTime(context, callBack, hintDay, beforeTime, endTime, true);
    }

    public interface TimerCallBack {
        void callBack(String time);
    }
}
